"""
Script to add scan angles into the SO2 column amounts file created
by Vitchko's SS_TO_STD retrievals script

"""

import wx
import os.path
import sys

from avoscan import processing

if __name__ == '__main__':
        
    app = wx.PySimpleApp()
    col_file = wx.FileSelector("Select Column amount or advanced retrieval results file.")
    
    if not col_file:
        sys.exit()
    
    corr_data_file = wx.FileSelector("Select Correction Data file.",default_path=os.path.dirname(col_file))
    
    if not corr_data_file:
        sys.exit()
    
    estimated_angle_dir = os.path.normpath(os.path.dirname(col_file)+"/"+os.path.pardir+"/"+os.path.pardir)
    angles_file = wx.FileSelector("Select Angles file.", default_path=estimated_angle_dir)
    
    if not angles_file:
        sys.exit()
    
    times_file = wx.FileSelector("Select times file.", default_path=estimated_angle_dir)
    #click cancel if you do not want a times file to be used
    if not times_file:
        times_file = None
    
    #TODO - compare the number of spectra listed in the times file with the number in the column amounts file.
    # If there is a difference then we know that the ss2std conversion script has skipped some. Then we should try
    #loading all the spectra to see which ones are unloadable. Could use the iterator for this.
    
    output_file = wx.FileSelector("Select output file.", flags=wx.FD_SAVE, default_path=os.path.dirname(col_file))
    if not output_file:
        sys.exit()
    
   # try:
    if col_file.endswith((".xls",".xlsx")):
        processing.create_angles_for_adv_retrieval(col_file, corr_data_file, angles_file, output_file, times_file=times_file)
    else:
        processing.create_angles_for_columns_file(col_file, corr_data_file, angles_file, output_file, times_file=times_file)
    
  #  except Exception,e:
   #     wx.MessageBox(str(e.args[0]), "",wx.ICON_ERROR)
        
    
    app.MainLoop()